package com.dayatang.hrm.compensation.infra.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.tapestry5.hibernate.HibernateSessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dayatang.commons.repository.EntityRepositoryHibernateTapestry;
import com.dayatang.hrm.compensation.domain.SalaryAccountSuite;
import com.dayatang.hrm.compensation.domain.SalaryAccountSuiteEmployeeAssign;
import com.dayatang.hrm.compensation.domain.SalaryAccountSuiteEmployeeAssignRepository;
import com.dayatang.hrm.staffing.domain.Employee;

@SuppressWarnings("unchecked")
public class SalaryAccountSuiteEmployeeAssignRepositoryImpl
		extends
		EntityRepositoryHibernateTapestry<SalaryAccountSuiteEmployeeAssign, Long>
		implements SalaryAccountSuiteEmployeeAssignRepository {

	private static final Logger logger = LoggerFactory
			.getLogger(SalaryAccountSuiteEmployeeAssignRepositoryImpl.class);

	public SalaryAccountSuiteEmployeeAssignRepositoryImpl(
			HibernateSessionManager sessionManager) {
		super(SalaryAccountSuiteEmployeeAssign.class, sessionManager);
	}

	@Override
	public List<Employee> findEmployeesBySalaryAccountSuite(
			SalaryAccountSuite salaryAccountSuite, Date queryDate) {
		return getSessionManager()
				.getSession()
				.getNamedQuery(
						"SalaryAccountSuiteEmployeeAssign.findEmployeesBySalaryAccountSuite")
				.setEntity("pSuite", salaryAccountSuite).setTimestamp(
						"pQueryDate", queryDate).list();
	}

	@Override
	public SalaryAccountSuiteEmployeeAssign getValid(
			SalaryAccountSuite salaryAccountSuite, Employee employee,
			Date queryDate) {

		List<SalaryAccountSuiteEmployeeAssign> assigns = getSessionManager()
				.getSession().getNamedQuery(
						"SalaryAccountSuiteEmployeeAssign.getValid").setEntity(
						"pSuite", salaryAccountSuite).setEntity("pEmp",
						employee).setTimestamp("pQueryDate", queryDate).list();

		if (assigns.isEmpty()) {
			return null;
		} else {
			return assigns.get(0);
		}

	}

	@Override
	public SalaryAccountSuite getValidSalaryAccountSuite(Employee employee,
			Date queryDate) {

		List<SalaryAccountSuite> suites = getSessionManager()
				.getSession()
				.getNamedQuery(
						"SalaryAccountSuiteEmployeeAssign.getValidSalaryAccountSuite")
				.setEntity("pEmp", employee).setTimestamp("pQueryDate",
						queryDate).list();

		if (suites.isEmpty()) {
			return null;
		} else {
			return suites.get(0);
		}

	}

	@Override
	public void terminate(SalaryAccountSuite salaryAccountSuite) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isAscriptionAlreadyExistExceptSelf(
			SalaryAccountSuite salaryAccountSuite, Employee employee,
			Date fromDate, Date toDate, SalaryAccountSuiteEmployeeAssign self) {

		boolean isDuplicate = false;

		List<SalaryAccountSuiteEmployeeAssign> assigns = new ArrayList<SalaryAccountSuiteEmployeeAssign>();

		if (logger.isDebugEnabled()) {

		}

		if (self == null) {
			assigns = getSessionManager()
					.getSession()
					.getNamedQuery(
							"SalaryAccountSuiteEmployeeAssign.isAscriptionAlreadyExistExceptSelf.selfIsNull")
					.setEntity("pEmp", employee).setTimestamp("pFromDate",
							fromDate).setTimestamp("pToDate", toDate).list();
		} else {
			assigns = getSessionManager()
					.getSession()
					.getNamedQuery(
							"SalaryAccountSuiteEmployeeAssign.isAscriptionAlreadyExistExceptSelf.selfIsExist")
					.setEntity("pEmp", employee).setEntity("pSelf", self)
					.setTimestamp("pFromDate", fromDate).setTimestamp(
							"pToDate", toDate).list();
		}

		if (!assigns.isEmpty()) {
			isDuplicate = true;
		}

		return isDuplicate;

	}

	@Override
	public List<SalaryAccountSuiteEmployeeAssign> findByDatePeriod(
			Date fromDate, Date toDate) {
		return getSessionManager().getSession().getNamedQuery(
				"SalaryAccountSuiteEmployeeAssign.findByDatePeriod")
				.setTimestamp("pFromDate", fromDate).setTimestamp("pToDate",
						toDate).list();
	}

}
